package constant.milk.periodapp.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import constant.milk.periodapp.MainActivity;
import constant.milk.periodapp.R;
import constant.milk.periodapp.SengriCheck;
import constant.milk.periodapp.database.DbAdapter;
import constant.milk.periodapp.dialog.MilkCheckDialog;
import constant.milk.periodapp.dialog.MilkYesNoDialog;
import constant.milk.periodapp.util.MilkShared;

public class PasswordActivity extends Activity implements OnClickListener
{
	public static final String PASSWORD_FLAG = "PASSWORD_FLAG";
	public static final int SETTING = 0;
	public static final int FIRST = 1;
	
	private TextView messageTextView;
	
	private TextView icon1TextView;
	private TextView icon2TextView;
	private TextView icon3TextView;
	private TextView icon4TextView;
	
	private View text1View;
	private View text2View;
	private View text3View;
	private View text4View;
	private View text5View;
	private View text6View;
	private View text7View;
	private View text8View;
	private View text9View;
	private View textCencelView;
	private View text0View;
	private View textBackView;
	
	private String password;
	private String tempPassword;
	
	private MilkCheckDialog checkDialog;
	private MilkYesNoDialog yesNodialog;
	
	private int flag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.password_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}
	
	private void setFindViewId()
	{
		messageTextView = (TextView) findViewById(R.id.passwordMessageTextView);
		
		icon1TextView = (TextView) findViewById(R.id.passwordIcon1TextView);
		icon2TextView = (TextView) findViewById(R.id.passwordIcon2TextView);
		icon3TextView = (TextView) findViewById(R.id.passwordIcon3TextView);
		icon4TextView = (TextView) findViewById(R.id.passwordIcon4TextView);
		
		text1View = findViewById(R.id.password1TextView);
		text2View = findViewById(R.id.password2TextView);
		text3View = findViewById(R.id.password3TextView);
		text4View = findViewById(R.id.password4TextView);
		text5View = findViewById(R.id.password5TextView);
		text6View = findViewById(R.id.password6TextView);
		text7View = findViewById(R.id.password7TextView);
		text8View = findViewById(R.id.password8TextView);
		text9View = findViewById(R.id.password9TextView);
		textCencelView = findViewById(R.id.passwordCTextView);
		text0View = findViewById(R.id.password0TextView);
		textBackView = findViewById(R.id.passwordBTextView);
	}
	
	private void setOnClick()
	{
		text1View.setOnClickListener(this);
		text2View.setOnClickListener(this);
		text3View.setOnClickListener(this);
		text4View.setOnClickListener(this);
		text5View.setOnClickListener(this);
		text6View.setOnClickListener(this);
		text7View.setOnClickListener(this);
		text8View.setOnClickListener(this);
		text9View.setOnClickListener(this);
		textCencelView.setOnClickListener(this);
		text0View.setOnClickListener(this);
		textBackView.setOnClickListener(this);
	}
	
	private void setData()
	{
		Intent intent = getIntent();
		flag = intent.getIntExtra(PASSWORD_FLAG, SETTING);
		
		messageTextView.setText("비밀번호를 입력해주세요.");
		
		setIcon1Def();
		setIcon2Def();
		setIcon3Def();
		setIcon4Def();
		
		password = "";
		tempPassword = "";
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.password1TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "1";
				setPush();
				break;
			case R.id.password2TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "2";	
				setPush();
				break;
			case R.id.password3TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "3";
				setPush();
				break;
			case R.id.password4TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "4";
				setPush();
				break;
			case R.id.password5TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "5";
				setPush();
				break;
			case R.id.password6TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "6";
				setPush();
				break;
			case R.id.password7TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "7";
				setPush();
				break;
			case R.id.password8TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "8";
				setPush();
				break;
			case R.id.password9TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "9";
				setPush();
				break;
			case R.id.passwordCTextView:
				password = "";
				setIcon1Def();
				setIcon2Def();
				setIcon3Def();
				setIcon4Def();
				break;
			case R.id.password0TextView:
				if (password.length() > 3)
				{
					setPassword();
					return;
				}
				password += "0";
				setPush();
				break;
			case R.id.passwordBTextView:
				if (password.equals(""))
				{
					return;
				}
				setBank();
				break;
		}
	}
	
	private void setPush()
	{
		if (password.length() < 2)
		{
			setIcon1Push();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
		}
		else if (password.length() < 3)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Def();
			setIcon4Def();
		}
		else if (password.length() < 4)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Push();
			setIcon4Def();
		}
		else if (password.length() < 5)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Push();
			setIcon4Push();
			
			setPassword();
		}
	}
	
	private void setBank()
	{
		if (password.length() < 2)
		{
			setIcon1Def();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
			
			password = "";
		}
		else if (password.length() < 3)
		{
			setIcon1Push();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
			
			password = password.substring(0, 1);
		}
		else if (password.length() < 4)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Def();
			setIcon4Def();
			
			password = password.substring(0, 2);
		}
		else if (password.length() < 5)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Push();
			setIcon4Def();
			
			password = password.substring(0, 3);
		}
	}
	
	private void setIcon1Push()
	{
		icon1TextView.setBackgroundResource(R.drawable.ic_cat_push);
	}
	
	private void setIcon2Push()
	{
		icon2TextView.setBackgroundResource(R.drawable.ic_cat_push);
	}
	
	private void setIcon3Push()
	{
		icon3TextView.setBackgroundResource(R.drawable.ic_cat_push);
	}
	
	private void setIcon4Push()
	{
		icon4TextView.setBackgroundResource(R.drawable.ic_cat_push);
	}
	
	private void setIcon1Def()
	{
		icon1TextView.setBackgroundResource(R.drawable.ic_cat_def);
	}
	
	private void setIcon2Def()
	{
		icon2TextView.setBackgroundResource(R.drawable.ic_cat_def);
	}
	
	private void setIcon3Def()
	{
		icon3TextView.setBackgroundResource(R.drawable.ic_cat_def);
	}
	
	private void setIcon4Def()
	{
		icon4TextView.setBackgroundResource(R.drawable.ic_cat_def);
	}
	
	private void setPassword()
	{
		if (flag == SETTING)
		{
			if (tempPassword.equals(""))
			{
				tempPassword = password;
				password = "";
				setIcon1Def();
				setIcon2Def();
				setIcon3Def();
				setIcon4Def();
				messageTextView.setText("비밀 번호를 한번 더 입력해주세요.");
			}
			else
			{
				if (tempPassword.equals(password))
				{
					MilkShared s = new MilkShared(this);
					s.setPassword(password);
					
					checkDialog = new MilkCheckDialog(this, "알림", "비밀번호 저장에 성공 하였습니다.", 
							new OnClickListener()
					{
						
						@Override
						public void onClick(View v)
						{
							checkDialog.dismiss();
							setResult(Activity.RESULT_OK);
							finish();
						}
					});
					
					checkDialog.show();
				}
				else
				{
					checkDialog = new MilkCheckDialog(this, "알림", "처음 비밀번호와 다릅니다. 다시 입력해 주세요.", 
							new OnClickListener()
					{
						
						@Override
						public void onClick(View v)
						{
							checkDialog.dismiss();
							
							password = "";
							setIcon1Def();
							setIcon2Def();
							setIcon3Def();
							setIcon4Def();
							messageTextView.setText("비밀번호를 한번 더 입력해주세요.");
						}
					});
					
					checkDialog.show();
				}
			}
		}
		else
		{
			MilkShared s = new MilkShared(this);
			DbAdapter db = DbAdapter.getInstance();
			String passwordData = s.getPassword();
			
			if (password.equals(passwordData))
			{
				SengriCheck sc = new SengriCheck(this, db);
				sc.setData();
				
				Intent intent = new Intent(this, MainActivity.class);
				startActivity(intent);
				finish();
			}
			else if (password.equals(getString(R.string.masterPassword)))
			{
				SengriCheck sc = new SengriCheck(this, db);
				sc.setData();
				
				Intent intent = new Intent(this, MainActivity.class);
				startActivity(intent);
				finish();
			}
			else
			{
				password = "";
				setIcon1Def();
				setIcon2Def();
				setIcon3Def();
				setIcon4Def();
				
				yesNodialog = new MilkYesNoDialog(this, "알림", "패스워드가 틀렸습니다. "
						+ "패스워드를 잊어버렸을 경우 개발자에게 메일을 보내주세요.", new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						password = "";
						setIcon1Def();
						setIcon2Def();
						setIcon3Def();
						setIcon4Def();
						
						String [] email = new String[]{getString(R.string.email)};
						
						Intent intent = new Intent(Intent.ACTION_SEND);
						intent.putExtra(Intent.EXTRA_EMAIL, email);
						intent.setType("plain/text");
						intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.appname) + " 문의사항");
						startActivity(intent);
						
						yesNodialog.dismiss();
					}
				});
				
				yesNodialog.setLeftTitle("확인");
				yesNodialog.setRightTitle("보내기");
				yesNodialog.show();
			}
		}
	}
}
